<?php

namespace App\Utils\API\Validation;

use App\Utils\API\Enum\ValidationEnum;
use Symfony\Bundle\MakerBundle\Str;

class ApiParamValidation {
    private $field = '';
    private $value = '';
    private $errors = [];

    /**
     * 执行验证
     * @param string $field
     * @param string $validation
     * @param string $value
     * @return array
     */
    public function do (string $field, string $validation, string $value) {
        $this->field = $field;
        $this->value = trim($value);
        $this->errors = [];
        // 拆分验证规则
        $validationGroups = explode('|', $validation);
        // 执行验证方法
        foreach ($validationGroups as $validation) {
            $this->validation($validation);
        }
        return $this->errors;
    }

    /**
     * 执行验证方法
     * @param string $content
     */
    public function validation (string $content) {
        $data = explode(':', $content);
        if (count($data) == 2) {
            $method = $data[0];
            $params = $data[1];
            $method = Str::asLowerCamelCase($method);
            if (method_exists($this, $method)) {
                $this->$method($params);
            }
        }
    }

    /**
     * 示范：in:a,b,c,10,20
     * @param string $content
     */
    private function in (string $content) {
        $enum = explode(',', $content);
        if (!in_array($this->value, $enum)) {
            array_push($this->errors, 'in:不是有效内容');
        }
    }

    /**
     * 示范：min:1
     * @param string $content
     */
    private function min (string $content) {
        if (is_numeric($content)) {
            $value = intval($this->value);
            $min = intval($content);
            if ($value < $min) {
                array_push($this->errors, '不能小于'.$min);
            }
        } else {
            array_push($this->errors, 'min validation error');
        }
    }

    /**
     * 示范：max:100
     * @param string $content
     */
    private function max (string $content) {
        if (is_numeric($content)) {
            $value = intval($this->value);
            $max = intval($content);
            if ($value > $max) {
                array_push($this->errors, '不能大于'.$max);
            }
        } else {
            array_push($this->errors, 'max validation error');
        }
    }

    /**
     * 示范：between:10,100
     * @param string $content
     */
    private function between (string $content) {
        $value = intval($this->value);
        $thresholds = explode(',', $content);
        if (count($thresholds) == 2) {
            $min = intval($thresholds[0]);
            $max = intval($thresholds[1]);
            if ($value < $min || $value > $max) {
                array_push($this->errors, '超出范围，只能在['.$content.']');
            }
        } else {
            array_push($this->errors, 'between validation error');
        }
    }

    /**
     * 示范：not_in:10,20,a,b,c
     * @param string $content
     */
    private function notIn (string $content) {
        $enum = explode(',', $content);
        if (in_array($this->value, $enum)) {
            array_push($this->errors, 'not_in:不是有效内容');
        }
    }

    /**
     * 示范：email:true
     * @param string $content
     */
    private function email (string $content) {
        if (!empty($this->value) && !preg_match('/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/', $this->value)) {
            array_push($this->errors, 'Email格式错误');
        }
    }

    /**
     * 示范：password:true
     * @param string $content
     */
    private function password (string $content) {
        if (!empty($this->value)) {
            if (!preg_match('/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]+$/', $this->value)) {
                array_push($this->errors, '密码应包含数字和字母');
            }
            if (strlen($this->value) < 6) {
                array_push($this->errors, '密码最少6个字符');
            }
            if (strlen($this->value) > 20) {
                array_push($this->errors, '密码最多20个字符');
            }
        }
    }

    /**
     * 示范：phone:true
     * @param string $content
     */
    private function phone (string $content) {
        if (!empty($this->value)) {
            if (!preg_match('/^1\d{10}$/', $this->value)) {
                array_push($this->errors, '手机号码格式错误');
            }
        }
    }

    /**
     * 示范：length:10
     * @param string $content
     */
    private function length (string $content) {
        if (!empty($this->value)) {
            if (is_numeric($content)) {
                $length = intval($content);
                if (strlen($this->value) != $length) {
                    array_push($this->errors, '长度必须为'.$length);
                }
            } else {
                array_push($this->errors, 'length validation error');
            }
        }
    }

    /**
     * 示范：enum:log_type_map
     * @param string $content
     */
    private function enum (string $content) {
        $sign = false;
        foreach (ValidationEnum::$types as $type => $map) {
            if ($type == $content) {
                $sign = true;
                if (!in_array($this->value, $map)) {
                    array_push($this->errors, 'enum:不是有效内容');
                }
                break;
            }
        }
        if (!$sign) {
            array_push($this->errors, 'not support enum:'.$content);
        }
    }

}